;(function () {
  Array.prototype.findArr = function() {
    var res = [this[0]];
    for (var i = 1; i < this.length; i++) {
      var repeat = false;
      for (var j = 0; j < res.length; j++) {
        if (this[i].projectId == res[j].projectId) {
          repeat = true;
        }
      }
      if (!repeat) {
        res.push(this[i]);
      }
    }
    return res;
  };

  new Vue({
    el: '#unit-mange-list',
    data: {
      project: [],
      building: [],
      floorData: [],
      burentunits: [],
      unitTypes: [
        {name: '普通单元', code: 'BU'},
        {name: '联合办公', code: 'WP'},
        {name: '停车场', code: 'ST'}
      ],
      unitType: [],
      totalPage: 0,
      totalCount: 0,
      currentPage: 1,
      limit: [{name: '10', code: '10'}, {name: '15', code: '15'}, {name: '20', code: '20'}],
      pageSize: ['10'],
      projectId: [],
      buildId: [],
      floorId: [],
      groupId:null,
      groupOneData:[],
      groupTwoData:[],
      groupThreeData:[],
      selectedGroupData:[],
      isExtenseFlag:false
    },
    filters: {
      businessTypev: function (str) {
        let result;
        switch (str) {
          case 'BU':
            result = '普通单元'
            break;
          case 'ST':
            result = '停车场'
            break;
          case 'WP':
            result = '联合办公'
            break;
          default:
            result = '--'
        }
        return result
      },
    },
    methods: {
      openSearch:function(){
        this.isExtenseFlag = this.isExtenseFlag == true ? false : true
      },
      selUnitType (arr) {
        this.unitType = [arr[0].code];
        this.currentPage = 1;
        this.getSearchList();
      },
      // 获取项目数据
      getProjectData:function(){
        var self = this;
        hlhttp.get({
          url:'user/projects',
          success:function(data){
            data = data.findArr();
            self.project = hlArrayUtil.replaceData(data, [{key: 'projectName', rKey: 'name'}, {key: 'projectId', rKey: 'code'}]);
            self.projectId = self.project[0]['code']

            let selectedPro = []
            selectedPro.push(self.project[0])

            self.getGroupPrimaryData(selectedPro)
            self.getSearchList()

          }
        })
      },
      // 获取一级组合数据
      getGroupPrimaryData(data){
        const self = this
        self.projectId = [data[0].code]
        self.groupId = null
        self.selectedGroupData = []
        self.getGroupData().then(data => {
          self.groupOneData = hlArrayUtil.replaceData( data, [{key: 'id', rKey: 'code'}] );
        })
      },
      // 获取二级组合数据
      selectOneGroup(res){
        const self = this
        self.groupId = res['cur']['code']
        self.getGroupData().then(data => {
          self.groupTwoData = hlArrayUtil.replaceData( data, [{key: 'id', rKey: 'code'}] )
          self.getBuildingData()
        })
      },
      // 获取三级组合数据
      selectTwoGroup(res){
        const self = this
        self.groupId = res['cur']['code']
        self.getGroupData().then(data => {
          self.groupThreeData = hlArrayUtil.replaceData( data, [{key: 'id', rKey: 'code'}] )
          self.getBuildingData()
        })
      },
      selectThreeGroup(res){
        this.groupId = res['cur']['code']
      },
      // 获取项目楼宇列表数据
      getBuildingData:function(){
        var self = this;
        this.buildId = [];
        this.floorId = [];
        this.floorData = [];

        hlhttp.get({
          url:'project/grouping/_building',
          data: { project_id: self.projectId[0] , grouping_id: self.groupId },
          success:function(data){
						data.unshift({buildingId: '#', name: '所有楼栋'})
            data = data.filter(el => el.configState == 'Y')
            self.building = hlArrayUtil.replaceData(data, [{key: 'buildingId', rKey: 'code'}]);
          }
        })

      },
      //获取项目楼层接口
      getFloorData: function (arr) {
        this.floorId = [];
        var self = this;
        hlhttp.get({
          url:'building/floors',
          data: {project_id: self.projectId[0], building_id: arr[0].code},
          success:function(data){
						data.unshift({floorId: '#', floorName: '所有楼层'})
            self.floorData = hlArrayUtil.replaceData(data, [{key: 'floorId', rKey: 'code'}, {key: 'floorName', rKey: 'name'}]);
          }
        })
      },
      //获取楼层id
      getFloorId: function (arr) {
        this.floorId = [arr[0].code];
      },
      //重置
      resetButton: function () {
        this.projectId = this.project[0]['code'].split()
        this.selectedGroupData = []
        this.groupOneData = []
        this.groupTwoData = []
        this.groupThreeData = []
        this.groupId = null
				this.buildId = []
				this.floorId = []
        this.unitType = [];
        this.currentPage = 1;

        this.getSearchList()
      },
      //查询
      search(){
        this.currentPage = 1
        this.pageSize = ['10'];
        this.getSearchList();
      },
      //搜索普通非自用的单元列表
      getSearchList: function () {
        var self = this;
        if (!self.projectId[0]) {
          hl.toast('请选择项目')
          return
        }

        hlhttp.get({
          url:'bu_rent_units/_search',
          data: {
            group_id: self.groupId || '#',
            project_id: self.projectId.length ? self.projectId[0] : '#',
            building_id: self.buildId.length ? self.buildId[0] : '#',
            floor_id: self.floorId.length ? self.floorId[0] : '#',
            page: self.currentPage,
            unit_type: self.unitType.length ? self.unitType[0] : '#',
            page_size: self.pageSize[0]
          },
          success:function(data){
            self.totalPage = data.totalPage
            self.totalCount = data.totalCount;
            self.burentunits = data.units;
          }
        })
      },
      //翻页
      fanye: function (arr) {
        this.pageSize = [arr[0].code]
        this.currentPage = 1
        this.getSearchList()
      },
      validateIsPro(){
        if(this.projectId.length == 0){
          hl.toast('请选择项目')
          return false
        }

        return true
      },
      getGroupData(){
        const self = this

        return new Promise(resolve => {
          hlhttp.get({
            url:'project/grouping_info/_subclass',
            data:{'project_id':self.projectId[0] , 'grouping_id':self.groupId || '#' },
            success:function(data){
              resolve(data)
            }
          })
        })
      }
    },
    mounted: function () {
      this.getProjectData()
      hlSelectUi()
    }
  })
} ())
